package kr.hkit.PowerJava.chapter15;

import javax.swing.JPanel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ChangePanel extends JPanel implements ActionListener {
	private JButton btnChangecolor;
    private Color color;
    private Mypanel panel;
	/**
	 * Create the panel.
	 */
	public ChangePanel() {

		initialize();
	}
	private void initialize() {
		setLayout(new BorderLayout(0, 0));
		color = new Color(0, 0, 0);
		btnChangecolor = new JButton("색상변경");
		btnChangecolor.addActionListener(this);
		add(btnChangecolor, BorderLayout.SOUTH);
		
		panel = new Mypanel();
		add(panel, BorderLayout.NORTH);
	}
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.setColor(color);
		g.fillRect(10, 10, 430, 430);
	//	color = new Color((int)(Math.random()*255.0), (int)(Math.random()*255.0),(int)(Math.random()*255.0));
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnChangecolor) {
			actionPerformedBtnChangecolor(e);
		}
	}
	protected void actionPerformedBtnChangecolor(ActionEvent e) {
		color = new Color((int)(Math.random()*255.0), (int)(Math.random()*255.0),(int)(Math.random()*255.0));
		repaint();
	}
}
